package com.jack.code.auto.service.impl;

import com.google.gson.Gson;
import com.jack.code.auto.domain.CreateCode;
import com.jack.code.auto.domain.request.CreateCodeRequestModel;
import com.jack.code.auto.mapper.CreateCodeMapper;
import com.jack.code.auto.service.CreateCodeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  代码生成器service
 * Created by LiJiakui on 17/3/2.
 */
@Service
public class CreateCodeServiceImpl implements CreateCodeService{

    @Resource
    private CreateCodeMapper createCodeMapper;


    /**
     * 列表
     */
    @Override
    public List<CreateCode> list() {
        return createCodeMapper.selectPageList();
    }

    @Override
    public void save(CreateCodeRequestModel requestModel) {
        CreateCode model = new CreateCode();
        Gson gson = new Gson();
        Map<String,Object> map = new HashMap();
        map.put("attrNames",gson.toJson(requestModel.getAttrNames()));
        map.put("attrTypes",gson.toJson(requestModel.getAttrTypes()));
        map.put("isInputs",gson.toJson(requestModel.getIsInputs()));
        map.put("attrLengths",gson.toJson(requestModel.getAttrLengths()));
        map.put("attrRemarks",gson.toJson(requestModel.getAttrRemarks()));
        map.put("defaultVals",gson.toJson(requestModel.getDefaultVals()));
        model.setFieldList(gson.toJson(map));
        model.setPackageName(requestModel.getPackageName());
        model.setObjectName(requestModel.getObjectName());

        model.setTableName(requestModel.getTableTop()==null?"":requestModel.getTableTop()+requestModel.getTableName());
        model.setCreateTime(new Date());
        model.setDescription(requestModel.getDescription());
        createCodeMapper.insert(model);
    }

    @Override
    public void delete(Integer id) {
        createCodeMapper.deleteByPrimaryKey(id);
    }
}
